package cn.dlc.com.order.controller;

import cn.dlc.com.order.entity.Product;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

/**
 * 通用降级处理类
 */
public class MyExceptionUtil {

    /**
     * 异常降级方法,这些参数是和ClientHttpRequestInterceptor中的interceptor一致的
     * @param request
     * @param body
     * @param clientHttpRequestExecution
     * @param exception
     * @return
     */
    public static SentinelClientHttpResponse fallback(
            HttpRequest request, byte[] body,
            ClientHttpRequestExecution clientHttpRequestExecution,
            BlockException exception){
        Product product = new Product();
        product.setDesc("RestTemplate出现未知异常调用的方法");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }

    /**
     * 限流降级方法,这些参数是和ClientHttpRequestInterceptor中的interceptor一致的
     * @param request
     * @param body
     * @param clientHttpRequestExecution
     * @param exception
     * @return
     */
    public static SentinelClientHttpResponse blockHandler(
            HttpRequest request, byte[] body,
            ClientHttpRequestExecution clientHttpRequestExecution,
            BlockException exception){
        Product product = new Product();
        product.setDesc("RestTemplate出现限流降级调用的方法");
        return new SentinelClientHttpResponse(JSON.toJSONString(product));
    }
}
